package com.easyadmin.pro.modules.sys.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.easyadmin.pro.common.constants.HttpConstants;
import com.easyadmin.pro.common.controller.BaseController;
import com.easyadmin.pro.common.vo.RestResult;
import com.easyadmin.pro.modules.sys.entity.UserPermissionsEntity;
import com.easyadmin.pro.modules.sys.entity.UserRoleEntity;
import com.easyadmin.pro.modules.sys.service.IUserPermissionsService;
import com.easyadmin.pro.modules.sys.service.IUserRoleService;
import com.easyadmin.pro.modules.sys.vo.RoleRelevance;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  控制层。
 *
 * @author xulin
 * @since 2024-04-02
 */
@RestController
@RequestMapping(HttpConstants.SYS_MODULE_PATH + "/userRole/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserRoleController extends BaseController {

    private final IUserRoleService iUserRoleService;

    private final IUserPermissionsService userPermissionsService;

    /**
     * 添加。
     *
     * @param userRoleEntity 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public RestResult save(@RequestBody UserRoleEntity userRoleEntity) {
        return success(iUserRoleService.save(userRoleEntity));
    }

    /**
     * 批量添加
     * @param userRoleEntities
     * @return
     */
    @PostMapping("saveBatch")
    public RestResult saveBatch(@RequestBody List<UserRoleEntity> userRoleEntities) {
        return success(iUserRoleService.saveBatch(userRoleEntities));
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public RestResult remove(@PathVariable Serializable id) {
        return success(iUserRoleService.removeById(id));
    }

    /**
     * 根据主键更新。
     *
     * @param userRoleEntity 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public RestResult update(@RequestBody UserRoleEntity userRoleEntity) {
        return success(iUserRoleService.updateById(userRoleEntity));
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public RestResult list() {
        return success(iUserRoleService.list());
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public UserRoleEntity getInfo(@PathVariable Serializable id) {
        return iUserRoleService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public RestResult page(Page<UserRoleEntity> page) {
        return success(iUserRoleService.page(page));
    }

    /**
     * 获取用户角色关联的路由信息
     * @return
     */
    @GetMapping("getUserRouterInfo/{roleId}")
    public RestResult getUserRouterInfo(@PathVariable Long roleId) {
        List<UserPermissionsEntity> userPermissionsEntities = userPermissionsService.list(new QueryWrapper()
                .eq(UserPermissionsEntity::getRoleId, roleId));
        List<String> routerIds = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userPermissionsEntities)) {
            routerIds = userPermissionsEntities.stream()
                    .map(e -> e.getRouterId().toString()).collect(Collectors.toList());
        }
        return success(routerIds);
    }

    /**
     * 用户关联角色
     * @param roleRelevance
     * @return
     */
    @PostMapping("roleRelevance")
    public RestResult roleRelevance(@RequestBody RoleRelevance roleRelevance) {
        iUserRoleService.roleRelevance(roleRelevance);
        return success();
    }

    /**
     * 获取已经关联的角色ID
     * @param userId
     * @return
     */
    @GetMapping("associatedRoleInfo/{userId}")
    public RestResult associatedRoleInfo(@PathVariable Long userId) {
        List<String> roleId = iUserRoleService.associatedRoleInfo(userId);
        return success(roleId);
    }
}
